package cn.com.shopec.erp.product.commo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.product.entity.Brand;
import cn.com.shopec.erp.product.vo.BrandVo;

/**
 * 
 * @ClassName: BrandVoConvertor
 * @Description: 品牌对象转换器
 * @author: duhui
 * @date 2013-5-26 下午04:49:22
 * 
 */
@Component
public class BrandVoConvertor {

    public BrandVo convert2BrandVo(Brand brand) {
        BrandVo brandVo = null;

        if (LogicUtil.isNotNull(brand)) {
            brandVo = new BrandVo();

            brandVo.setBrandNo(brand.getBrandNo());
            brandVo.setBrandUrl(brand.getBrandUrl());
            brandVo.setBrandName(brand.getBrandName());
            brandVo.setDescription(brand.getDescription());
            brandVo.setDeleteFlag(brand.getDeleteFlag());
            brandVo.setNo(brand.getNo());
        }

        return brandVo;
    }

    public List<BrandVo> convert2BrandVos(List<Brand> brandList) {
        List<BrandVo> brandVos = null;

        if (LogicUtil.isNotNullAndEmpty(brandList)) {
            brandVos = new ArrayList<BrandVo>();

            for (Brand brand : brandList) {
                BrandVo brandVo = this.convert2BrandVo(brand);
                brandVos.add(brandVo);
            }
        }

        return brandVos;
    }

    public Brand convert2Brand(BrandVo brandVo) {
        Brand brand = null;

        if (LogicUtil.isNotNull(brandVo)) {
            brand = new Brand();

            brand.setBrandNo(brandVo.getBrandNo());
            brand.setBrandName(brandVo.getBrandName());
            brand.setBrandUrl(brandVo.getBrandUrl());
            brand.setDescription(brandVo.getDescription());
            brand.setNo(brandVo.getNo());
            if (brandVo.getDeleteFlag() == null) {
                brand.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
            } else {
                brand.setDeleteFlag(brandVo.getDeleteFlag());
            }
        }

        return brand;
    }

    public Brand convert2Brand(BrandVo brandVo, Brand brand) {

        if (LogicUtil.isNotNull(brandVo) && LogicUtil.isNotNull(brand)) {
            brand.setBrandName(brandVo.getBrandName());
            brand.setBrandUrl(brandVo.getBrandUrl());
            brand.setDescription(brandVo.getDescription());
            brand.setUpdateTime(DateUtil.formatTimesTampDate(new Date()));
            brand.setNo(brandVo.getNo());

            return brand;
        } else {
            return null;
        }
    }

    public PageFinder<BrandVo> convert2PagedBrandVos(PageFinder<Brand> pagedBrands) {
        PageFinder<BrandVo> pagedBrandVos = null;

        if (LogicUtil.isNotNull(pagedBrands.getData())) {
            List<BrandVo> brandVos = this.convert2BrandVos(pagedBrands.getData());

            pagedBrandVos = new PageFinder<BrandVo>(pagedBrands.getPageNo(), pagedBrands.getPageSize(),
                    pagedBrands.getRowCount(), brandVos);
        }

        return pagedBrandVos;
    }
}
